package com.inke.box.net.minasocket;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.inke.box.net.minasocket.vo.InkeMessage;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * Created by Administrator on 2017/11/8.
 */

public class InkeClientSocketHanlder extends IoHandlerAdapter {

    private MinaClientConnCallBack minaClientConnCallBack;

    public InkeClientSocketHanlder(MinaClientConnCallBack minaClientConnCallBack){
        this.minaClientConnCallBack = minaClientConnCallBack;
    }

    /**
     * 客户端与服务器链接后
     * */
    @Override
    public void sessionOpened(IoSession session) throws Exception {
        super.sessionOpened(session);
        LogUtils.e("已连接服务器");
//        InkeMessage inkeMessage = new InkeMessage();
//        inkeMessage.setName("hi");
//        session.write(inkeMessage);

        //连接成功后回调界面对象
        if (minaClientConnCallBack!=null) {
            minaClientConnCallBack.onConnectSucceend(session);
        }


    }

    /***
     * 客户端关闭
     * */
    @Override
    public void sessionClosed(IoSession session) throws Exception {
        super.sessionClosed(session);
        LogUtils.e("Im client 关闭");
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        super.messageReceived(session, message);
//        InkeMessage im=(InkeMessage)message;
        LogUtils.e("接收到消息:"+message.toString());
        ToastUtils.showShort(message.toString());

    }
}
